package com.likeycy.my.sort.cmp;

/**
 * @ClassName: BubbleSort2
 * @Description: 冒泡排序第三种写法
 * @Author: sodagreen
 * @Date: 2021/3/27 6:56
 * @Version: 1.0
 */
public class BubbleSort2<T extends Comparable<T>> extends Sort<T> {

    /**
     * 冒泡排序优化第二版，如果局部有序，打个标记
     */
    @Override
    protected void sort() {
        for (int end = array.length - 1; end > 0; end--) {
            int sortedIndex = 1;
            for (int begin = 1; begin <= end; begin++) {
                if (cmp(begin, begin - 1) < 0) {
                    swap(begin, begin-1);
                    sortedIndex = begin;
                }
            }
            end = sortedIndex;
        }
    }

}
